Series

Series is a one-dimensional labeled array capable of holding any data type (integers, strings, floating point numbers, Python objects, etc.). The axis labels are collectively referred to as the index.

documentation: http://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.html


In [2]:
import pandas as pd
import numpy as np
Create series from NumPy array

number of labels in 'index' must be the same as the number of elements in array


In [3]:
my_simple_series = pd.Series(np.random.randn(5), index=['a', 'b', 'c', 'd', 'e'])
my_simple_series


Out[3]:
a    0.591872
b   -1.381102
c    2.354536
d   -1.339378
e   -1.888910
dtype: float64

In [4]:
my_simple_series.index


Out[4]:
Index(['a', 'b', 'c', 'd', 'e'], dtype='object')
Create series from NumPy array, without explicit index

In [5]:
my_simple_series = pd.Series(np.random.randn(5))
my_simple_series


Out[5]:
0   -0.839923
1    0.310805
2   -0.491671
3   -0.418519
4   -1.785353
dtype: float64

Access a series like a NumPy array


In [6]:
my_simple_series[:3]


Out[6]:
0   -0.839923
1    0.310805
2   -0.491671
dtype: float64
Create series from Python dictionary

In [7]:
my_dictionary = {'a' : 45., 'b' : -19.5, 'c' : 4444}
my_second_series = pd.Series(my_dictionary)
my_second_series


Out[7]:
a      45.0
b     -19.5
c    4444.0
dtype: float64

Access a series like a dictionary


In [8]:
my_second_series['b']


Out[8]:
-19.5

note order in display; same as order in "index"

note NaN


In [9]:
pd.Series(my_dictionary, index=['b', 'c', 'd', 'a'])


Out[9]:
b     -19.5
c    4444.0
d       NaN
a      45.0
dtype: float64

In [10]:
my_second_series.get('a')


Out[10]:
45.0

In [11]:
unknown = my_second_series.get('f')
type(unknown)


Out[11]:
NoneType
Create series from scalar

If data is a scalar value, an index must be provided. The value will be repeated to match the length of index


In [12]:
pd.Series(5., index=['a', 'b', 'c', 'd', 'e'])


Out[12]:
a    5.0
b    5.0
c    5.0
d    5.0
e    5.0
dtype: float64

In [ ]: